package com.dxy.app.widget.universal_view;

import android.content.Context;
import android.graphics.Rect;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

/**
 * Created by dell on 2017/8/22.
 */

public class HidenBottomScrollView extends NestedScrollView {
  public HidenBottomScrollView(Context context) {
    super(context);
  }

  public HidenBottomScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public HidenBottomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  private int root;
  private int bottom;
  private int screenHeight;
  private int statusBarHeight;
  private int maxScrollHeight;//最大可滚动的高度
  public static final String TAG="TAG_Hiden";
  private View child;
  private Rect rectBottom;
  @Override public void onAttachedToWindow() {
    super.onAttachedToWindow();
    child =getChildAt(0);
    statusBarHeight =getStatusBarHeight(getContext());
    screenHeight =getScreenHeight(getContext());

    Log.i(TAG, "onAttachedToWindow: "+statusBarHeight+","+screenHeight);
    if(child instanceof ViewGroup){
      ViewTreeObserver childOb =child.getViewTreeObserver();
      childOb.addOnPreDrawListener(()->{
        if(root==0){
          root =child.getMeasuredHeight();
          maxScrollHeight =root -screenHeight;
          maxScrollHeight +=statusBarHeight;
          Log.i(TAG, "root height: hole view height:"+root+",max scroll height but not contain bottom:"+maxScrollHeight);
        }
        return true;
      });
      View last =((ViewGroup) child).getChildAt(((ViewGroup) child).getChildCount()-1);
      ViewTreeObserver observer =last.getViewTreeObserver();
      observer.addOnPreDrawListener(()->{
        if(bottom==0){
          bottom =last.getMeasuredHeight();
          maxScrollHeight -=bottom;
          Log.i(TAG, "last bottom height: max scroll height:"+maxScrollHeight+",bottom view height:"+bottom);
        }
        return true;
      });
    }
  }

  @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    super.onScrollChanged(l, t, oldl, oldt);
    Log.i(TAG, "onScrollChanged: new:"+l+",old:"+oldl+",newt:"+t+",oldt:"+oldt);
    if(t>maxScrollHeight){
      Log.i(TAG, "onScrollChanged: "+maxScrollHeight+","+t);
      if(!inTouchState){
        smoothScrollTo(0,maxScrollHeight);
      }else{
        curScrollHeight=t;
      }
    }
  }
  private int curScrollHeight;//当前滚动的距离


  private int getStatusBarHeight(Context context) {
    int result = 0;
    int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
      result = context.getResources().getDimensionPixelSize(resourceId);
    }
    return result;
  }
  private int getScreenHeight(Context context){
    int height =context.getResources().getDisplayMetrics().heightPixels;
    return height;
  }
  private boolean inTouchState =false;
  @Override public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction()){
      case MotionEvent.ACTION_DOWN:
        inTouchState =true;
        break;
      case MotionEvent.ACTION_UP:
        inTouchState =false;
        // TODO: 2017/8/22 当滑动停止时手指尚未抬起的情况也需要处理
        if(root-screenHeight>curScrollHeight){
          Log.i(TAG, "onTouchEvent: "+"the bottom is visible!!!");
          scrollTo(0,maxScrollHeight);
          curScrollHeight=0;
        }
        break;
    }
    return super.onTouchEvent(ev);
  }
}
